// pages/print/papersSearch/papersSearch.js
var utils = require("../../../utils/utils.js");
const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        title: '',
        pid: '',
        searchList: []
    },
    keywordInput(e) {
        this.setData({
            title: e.detail.value
        })
        this.searchKeyWord(this.data.title)
    },
    chooseItem(e) {
        var title = e.currentTarget.dataset.title;
        var pid = e.currentTarget.dataset.pid;
        this.setData({
            title: title,
            pid: pid
        })
        this.searchKeyWord(title)
    },
    searchKeyWord(val) {
        var that = this;
        utils.request('idPhoto/photoSpecLists', {
            keywords: val,
            'page': 1,
            'limit': 1000
        }, function (res) {
            if (res.data.code == 200) {
                that.setData({
                    searchList: res.data.data.rows
                })
            } else {
                that.setData({
                    searchList: []
                })
            }
        })
    },
    changeIndex(e) {
        var title = e.currentTarget.dataset.title;
        var pid = e.currentTarget.dataset.pid;
        this.setData({
            title: title,
            pid: pid
        })
        this.choosePhoto()
    },
    choosePhoto() {
        var that = this;
        wx.showActionSheet({
            itemList: ['相册', '拍照', '从QQ会话选择'],
            success(res) {
                if (res.tapIndex == 0) {
                    that.getPhoto('album');
                }
                if (res.tapIndex == 1) {
                    that.getPhoto('camera');
                    // wx.redirectTo({
                    //     url: '../personCamera/personCamera?code=31179418&w=245&h=357&picType=' + that.data.title + '&pid=' + that.data.pid
                    // })
                }
                if (res.tapIndex == 2) {
                    that.getPhoto1();
                }
            },
            fail(res) {

            }
        })
    },
    //拍照调取相册
    getPhoto(type) {
        var that = this;
        wx.chooseImage({
      count: 1,
      sizeType: ['original'],
      sourceType: [type],
      success(res) {
           const tempFilePaths = res.tempFilePaths[0];
           wx.showLoading()
                that.uploadImgFile(tempFilePaths)
                // wx.redirectTo({
                //     url: '../photoEdit/photoEdit?code=' + code + '&w=245&h=357&img=' + tempFilePaths + '&picType=' + that.data.title + '&pid=' + that.data.pid
                // })
            }
        })
    },
    getPhoto1() {
        var that = this;
        wx.chooseMessageFile({
            count: 1,
            type: 'image',
            success(res) {
                wx.showLoading()
                that.uploadImgFile(res.tempFiles[0].path)
                // wx.redirectTo({
                //     url: '../photoEdit/photoEdit?code=' + code + '&w=245&h=357&img=' + res.tempFiles[0].path + '&picType=' + that.data.title + '&pid=' + that.data.pid
                // })
            }
        })
    },
    //上传图片
    uploadImgFile(path) {
        var that = this;
        utils.uploadImg(path,function (res) {
            wx.hideLoading()
            var data = JSON.parse(res.data).data
            wx.navigateTo({
                url: '../papersEdit/papersEdit?&url='+data.fileUrl+'&pid='+that.data.pid+'&serviceMainId='+that.data.serviceMainId+'&tagFloor='+that.data.tagFloor+'&title='+that.data.title
            })
        })
    },
    getIndex() {
        var that = this;
        utils.request('idPhoto/photoSpecLists', {
            keywords: '',
            'page': 1,
            'limit': 20
        }, function (res) {
            if (res.data.code == 200) {
                that.setData({
                    list: res.data.data.rows
                })
            } else {
                utils.toast(res.data.msg)
            }
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.setData({
            tagFloor:options.tagFloor,
            serviceMainId:options.serviceMainId
        })
        this.getIndex()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    }
})